#include "head_s.h"

int cd(user_t* status, const portocol_t* portocol, int net_fd, MYSQL* mysql)
{

    char path_root[1024] = {0};
    int flag_path = 0;
    sprintf(path_root, "%s%s%s", status->base_path, status->user_name, status->path);
    char path_s[1024] = {0};
    strcpy(path_s, status->arg);


    // 文件所在的路径
    char path_s2[1024] = {0};
    strcpy(path_s2, status->path);
    path_s2[strlen(path_s2)-1]=0;

    // 获取用户id
    char nameId[1024] = {0};
    sprintf(nameId, "select id from user_info where name = '%s'", status->user_name);
    mysql_query(mysql, nameId);
    MYSQL_RES* res_usr = mysql_store_result(mysql);
    if(res_usr == 0){
        mysql_error(mysql);
        exit(1);
    }
    MYSQL_ROW row2;
    int usr_id = 0;
    while((row2 = mysql_fetch_row(res_usr))){
        char num[10];
        strncpy(num, row2[0], sizeof(num) - 1);
        num[sizeof(num) - 1] = '\0';
        usr_id = atoi(num);
    }
    mysql_free_result(res_usr);
    if(strcmp(path_s, "..") == 0){
        // 文件需要回退到上级目录
        // 需要：父id，父id->id 的路径
        // 根据 path_s 获取父id
        flag_path = 2;
        send(net_fd, &flag_path, sizeof(int), 0);
        char uid[1024] = {0};
        sprintf(uid, "select p_id from file_info where file_path = '%s' and user_id = %d", path_s2, usr_id);
        mysql_query(mysql, uid);
        MYSQL_RES* res = mysql_store_result(mysql);
        if(res == 0){
            mysql_error(mysql);
            exit(1);
        }
        MYSQL_ROW row1;
        int point_id = 0;
        while((row1 = mysql_fetch_row(res))){
            char point[10] = {0};
            strncpy(point, row1[0], sizeof(point));
            point[sizeof(point) - 1] = '\0';
            point_id = atoi(point);
        }
        mysql_free_result(res);

        char path[50] = {0};
        sprintf(path, "select file_path from file_info where id = %d", point_id);
        mysql_query(mysql, path);
        MYSQL_RES* res_path = mysql_store_result(mysql);
        if(res_path == 0){
            mysql_error(mysql);
            exit(1);
        }
        MYSQL_ROW row2;
        char point_path[1024];
        while((row2 = mysql_fetch_row(res_path))){
            strncpy(point_path, row2[0], sizeof(point_path));
            point_path[sizeof(point_path) - 1] = '\0';
        }
        mysql_free_result(res_path);

        send(net_fd, point_path, sizeof(point_path), MSG_NOSIGNAL);

    }else if(strcmp(path_s, ".") == 0){
        // send(net_fd, path_i, sizeof(path_i), MSG_NOSIGNAL);
        flag_path = 1;
        send(net_fd, &flag_path, sizeof(int), 0);
    }else if(strcmp(path_s, "/") == 0 || strlen(status->arg) == 0){
        flag_path = 3;
        send(net_fd, &flag_path, sizeof(int), 0);
        // char sym[10] = "/";
        // send(net_fd, sym, sizeof(sym), MSG_NOSIGNAL);
        // send(net_fd, path_root, sizeof(path_root), MSG_NOSIGNAL);
    }else if(strchr(path_s, '/') == NULL){
        // 仅仅cd一层，判断一个文件是否存在即可
        char file_name[50] = {0};


        char command2[1024] = {0};
        strcpy(file_name, path_s);
        sprintf(command2, "select file_type from file_info where file_name = '%s' and user_id = %d", file_name, usr_id);
        mysql_query(mysql, command2);
        MYSQL_RES* res_type = mysql_store_result(mysql);
        if(res_type == 0){
            mysql_error(mysql);
            exit(1);
        }
        MYSQL_ROW row_t;
        while((row_t = mysql_fetch_row(res_type))){
            if(strcmp(row_t[0], "2") == 0){
                flag_path = 4;
                send(net_fd, &flag_path, sizeof(int), 0);
                mysql_free_result(res_type);
                return 0;
            }
        }
        mysql_free_result(res_type);


        flag_path = 0;
        send(net_fd, &flag_path, sizeof(int), 0);
        char command[1024] = {0};
        // strcpy(file_name, path_s);
        sprintf(command, "select exist_flag from file_info where file_name = '%s'", file_name);
        mysql_query(mysql, command);
        MYSQL_RES* res_name = mysql_store_result(mysql);
        if(res_name == 0){
            mysql_error(mysql);
            exit(1);
        }
        MYSQL_ROW row3;
        int flag = 0;
        while((row3 = mysql_fetch_row(res_name))){
            char name_flag[10] = {0};
            strncpy(name_flag, row3[0], sizeof(name_flag) - 1);
            name_flag[sizeof(name_flag) - 1] = '\0';
            flag = atoi(name_flag);
        }
        if(flag == 0){
            // 文件存在
            send(net_fd, path_s, sizeof(path_s), MSG_NOSIGNAL);
        }else{
            // flag=1，文件删除
            return 0;
        }
        mysql_free_result(res_name);
    }else{
        // 长路径，需要看每次分割后的文件是否存在
        char name2[1024] = {0};
        char mont[1024] = {0};
        strcpy(name2, path_s);
        strcat(name2, "/");
        // 初始化分割后存放每个文件名的变量，sql语句，是否存在判断变量
        char* p;
        strcpy(mont, status->path);
        char sql[1024] = {0};
        int flag2 = 3;
        // 开始分割路径，得到多个文件名
        for(p = strtok(name2, "/"); p != NULL; p = strtok(NULL, "/")){
            bzero(sql, sizeof(sql));
            if(strlen(mont) !=1 ){
                strcat(mont, "/");
            }
            strcat(mont, p);
            // sql得到文件类型 1 or 2
            char name_l[10] = {0};
            strcpy(name_l, p);
            sprintf(sql, "select file_type from file_info where file_name = '%s' and user_id = %d and file_path = '%s'", name_l, usr_id, mont);
            mysql_query(mysql, sql);
            printf("%s\n", mysql_error(mysql));
            MYSQL_RES* res_long = mysql_store_result(mysql);
            if(res_long == 0){
                mysql_error(mysql);
                exit(1);
            }
            MYSQL_ROW row_long;
            while((row_long = mysql_fetch_row(res_long))){
                if(strcmp(row_long[0], "2") == 0){
                    flag_path = 4;
                    send(net_fd, &flag_path, sizeof(int), 0);
                    mysql_free_result(res_long);
                    return 0;
                }
            }

            // 保证文件权威目录，可以 cd 到
            // flag_path = 0;

            // 查找目录是否存在：0 or 1
            sprintf(sql, "select exist_flag from file_info where file_path = '%s' and user_id = %d", mont, usr_id);
            mysql_query(mysql, sql);
            printf("%s\n", mysql_error(mysql));
            MYSQL_RES* res_name2 = mysql_store_result(mysql);
            if(res_name2 == 0){
                mysql_error(mysql);
                exit(1);
            }
            MYSQL_ROW row4;
            while((row4 = mysql_fetch_row(res_name2))){
                char name_flag2[10] = {0};
                strncpy(name_flag2, row4[0], sizeof(name_flag2) - 1);
                name_flag2[sizeof(name_flag2) - 1] = '\0';
                flag2 = atoi(name_flag2);
            }
            mysql_free_result(res_name2);
        }

        send(net_fd, &flag_path, sizeof(int), 0);
        if(flag2 == 0){
            // 文件存在
            // strcat(path_s, "/");
            send(net_fd, path_s, sizeof(path_s), MSG_NOSIGNAL);

        }else{
            // flag=1，文件删除
            return 0;
        }
    }

}

